<?php
class OcmMenuController extends FW24H_AppController{

	function index() {
		//$this->checkPermission( 'read');

		fw24h_useModel( 'menu');
		$objModel = new MenuModel();
		// Listing menu
		if($arrMenu = $objModel->categoryArray()){
			$this->setParam('arrMenu', $arrMenu);
		}
		$this->render();
	}

	function moveup(){
		$this->checkPermission( 'edit', 'menu/index');

		$this->Menu->moveUp( intval($this->_path_info[0]));

		$this->Log->insertLog( 'MENU_INDEX_MOVEUP', '4/7/2009', 'menu/moveup');

		$this->redirect( BASE_URL.'/menu/index');
		//$this->render();
	}
	function movedown(){
		$this->checkPermission( 'edit', 'menu/index');

		$this->Menu->moveDown( intval( $this->_path_info[0]));
		$this->redirect( BASE_URL.'/menu/index');
		//$this->render();
	}
	// add menu
	function add(){
		$this->checkPermission( 'create', 'menu/index');
		
		if(isset($_POST['gone'])){
			if(!fw24h_validate( $_POST['menu_name'], VALID_NOT_EMPTY)){
				$this->setParam( 'error', 'Tên menu không được bỏ trống');
			}
			elseif(!fw24h_validate( $_POST['menu_link'], VALID_NOT_EMPTY)){
				$this->setParam( 'error', 'Link menu không được bỏ trống');
			}
			else{
				// add menu
				$this->Menu->addMenu();
				$this->redirect( BASE_URL.'/menu/add');
			}
		}
		$this->setParam( 'parentMenu', $this->Menu->parentMenu());
		$this->render();
	}
	// update menu
	function edit(){
		$this->checkPermission( 'edit', 'menu/index');

		if(isset($_POST['gone'])){
			if(!fw24h_validate( $_POST['menu_name'], VALID_NOT_EMPTY)){
				$this->setParam('error', 'Tên menu không được bỏ trống');
			}
			elseif(!fw24h_validate( $_POST['menu_link'], VALID_NOT_EMPTY)){
				$this->setParam('error', 'Link menu không được bỏ trống');
			}
			else{
				// add menu
				$this->Menu->updateMenu(intval($this->_path_info[0]), $_POST);
				$this->redirect(BASE_URL.'/menu/index');
			}
		}
		
		$this->setParam('parentMenu',$this->Menu->parentMenu());
		$this->setParam('menuInfo', $this->Menu->menuInfo(intval($this->_path_info[0])));
		$this->render();
	}
	// active menu
	function active(){
		$this->checkPermission( 'edit', 'menu/index');

		$this->Menu->changeStatus(intval($this->_path_info[0]), 1);
		$this->redirect(BASE_URL.'/menu/index');
	}
	// Deactive menu
	function deactive(){
		$this->checkPermission( 'edit', 'menu/index');

		$this->Menu->changeStatus(intval($this->_path_info[0]), 0);
		$this->redirect(BASE_URL.'/menu/index');
	}
}

